import copy
from django.template import Library
from django.conf import settings

register = Library()


@register.inclusion_tag("tag/menu.html")
def menu(request):
    # 1.获取角色及其对应菜单
    role = request.user_info["role"]
    menu = copy.deepcopy(settings.MENU[role])

    # 2.选中显示
    curr_url_name = request.resolver_match.url_name
    for item in menu:
        for child in item["children"]:
            if curr_url_name == child["name"]:
                child["class"] = "active"

    # 3.渲染
    return {"menu": menu}
